//
//  WBHomeViewModel.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/16.
//  Copyright © 2016年 木木. All rights reserved.
//

import UIKit
import SDWebImage

class WBHomeViewModel: NSObject {

    lazy var modelViewArr:[WBStatuesViewModel] = [WBStatuesViewModel]()
    
    //获取接口数据
    func getData(isPluup:Bool ,finished:@escaping(Bool,Int)->()){
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let access_token = WBUserAccessViewModel.shared.userAccount?.access_token
        
        var max_id :Int64 = 0
        var since_id :Int64 = 0
        if isPluup {
             max_id = modelViewArr.last?.viewmodel?.id ?? 0
            
            if max_id != 0 {
//                max_id = max_id - 1
                max_id -= 1
            }
        }else{
            since_id = modelViewArr.first?.viewmodel?.id ?? 0
        }
   
        let dict = ["access_token":access_token,
                          "max_id":"\(max_id)",
                           "since_id":"\(since_id)"]
        HMNetworkTools.shared.request(method: .GET, urlString: urlString, parameters: dict) { (responsedObject, error) in
            
            if error != nil {
                finished(false,0)
                return
            }
            let dict = responsedObject as! [String:Any]
            
            guard let array = dict["statuses"] as? [[String:Any]]  else{
                 finished(false,0)
                return
            }
            
            var tempARR = [WBStatuesViewModel]()
//            tempARR = NSArray.yy_modelArray(with: WBStatuses.self , json: array) as! [WBStatuses]
            for item in array{
                let viewmodel = WBStatuesViewModel()
                //item 是一个对象
                let S = WBStatuses()
                S.yy_modelSet(with: item)
                viewmodel.viewmodel = S
                tempARR.append(viewmodel)
        
            }
            //MARK:
            // MARK: - 传值
            //上拉刷新
            if  isPluup{
                self.modelViewArr = self.modelViewArr + tempARR
//                print(tempARR)
            }else{
                //下拉,或者第一次加载
                self.modelViewArr = tempARR + self.modelViewArr

            }
//             finished(true,tempARR.count)
            self.cacheSingleImage(array: tempARR, finished: finished)
            
        }
    }
    
    
    private func cacheSingleImage(array:[WBStatuesViewModel],finished:@escaping(Bool,Int)->()){
        if array.count == 0{
            //执行回调
            finished(true,0)
            return
        }
        //遍历视图模型   ------->使用调度组
        let group = DispatchGroup()
        for MODE in array {
            if MODE.urlStringS?.count == 1 {
                //只有一张图片
                //执行异步下载图片的操作,在异步任务开始之前,执行入组操作
                group.enter()
                let  url = URL(string: MODE.urlStringS?.first?.wap360 ?? "")
                SDWebImageManager.shared().downloadImage(with: url, options: [], progress: nil, completed: { (_, _, _, _, _) in
                    //图片下载成功
                    //出🐷
                    group.leave()
                    
                })
            }
        }
        //执行回调
        group.notify(queue: DispatchQueue.main) {
            finished(true,array.count)
        }
        
    }
   
}

















